package com.zj.study.pojo;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import org.springframework.lang.NonNull;

/**
 * @author 赵赳
 * @since 2021/9/17 14:13
 */
@Getter
@ToString
@AllArgsConstructor
public enum RespStatusEnum {

  /**
   * OK：操作成功
   */
  OK("0000", "操作成功"),

  /**
   * 客户端
   */
  CLIENT_ERROR("A0001", "客户端操作问题"),
  CLIENT_BAD_PARAMETERS("A0100", "客户端参数错误"),
  CLIENT_REQUEST_REPEAT("A0200", "客户端重复提交"),

  /**
   * 系统
   */
  INTERNAL_SERVICE_ERROR("B0001", "系统执行出错"),
  INTERNAL_SERVICE_TIME_OUT("B0100", "系统执行超时"),
  INTERNAL_SERVICE_DEGRADE("B0200", "系统容灾触发"),
  INTERNAL_SERVICE_RATE_LIMIT("B0210", "系统限流"),
  INTERNAL_SERVICE_FALLBACK("B0220", "系统功能降级"),
  MIDDLEWARE_ERROR("B0300", "中间件服务出错"),
  OPERATION_FORBIDDEN("B0403", "无权限操作"),
  RESOURCE_NOT_FOUND("B0404", "资源不存在"),
  RESOURCE_EXISTED("B0405", "资源已存在"),
  RESOURCE_CONFLICT("B0409", "资源冲突"),

  /**
   * 外部服务
   */
  EXTERNAL_SERVICE_ERROR("C0001", "调用第三方服务出错"),
  EXTERNAL_SERVICE_TIME_OUT("C0100", "系统执行超时"),
  EXTERNAL_SERVICE_DEGRADE("C0200", "系统容灾触发"),
  EXTERNAL_SERVICE_RATE_LIMIT("C0210", "系统限流"),

  /**
   * 用户类
   */
  ACCOUNT_DISABLED("D0001", "账号已被禁用"),
  UN_LOGIN("D0002","尚未登录")
  ;

  /**
   * 响应状态
   */
  @NonNull
  private final String code;
  /**
   * 响应编码
   */
  @NonNull
  private final String msg;

  private final static Map<String, RespStatusEnum> ALL_STATUS = new HashMap<>();

  static {
    Arrays.stream(RespStatusEnum.values())
        .forEach(status -> ALL_STATUS.put(status.getCode(), status));
  }

  public static Optional<RespStatusEnum> fromCode(@NonNull String code) {
    return Optional.ofNullable(ALL_STATUS.get(code));
  }



}
